# GP.add_event_detect(pin_btn,GP.BOTH,callback=my_callback)
# pin 11:G17 button
# pin 12:G18 red led
# pin 13:G27 green led
import RPi.GPIO as GP
import time

pin_green = 13
pin_btn = 11
pin_red = 12

def start():
    GP.setmode(GP.BOARD)
    GP.setwarnings(False)
    GP.setup(pin_btn, GP.IN,pull_up_down=GP.PUD_UP)
    GP.setup(pin_red, GP.OUT)
    GP.add_event_detect(pin_btn,GP.BOTH,callback=my_callback)

def turnon(x):
    GP.output(pin_red,1)
    print('on')
    
def turnoff(x):
    GP.output(pin_red,0)
    print('off')

    
def my_callback(x):
    # x是回调函数自动给的参数,是探测到检测到信号变化的pin数值
    # print(GP.input(pin_btn))
    if not GP.input(pin_btn): # 按下状态
    # if x: # 按下状态
        # turnon(x)
        turnon(x)
    else:
        turnoff(x)

def loop():
    while True:
        pass
        # if not GP.input(pin_btn): # 按下状态
        #     GP.output(pin_red,1)
        # else:
        #     GP.output(pin_red,0)

def close():
    GP.output(pin_red,0)
    GP.cleanup()


if __name__ == '__main__':
    start()
    try:
        loop()
    except Exception as e:
        print(e)
        close()

